import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:tihai/entity/user.dart';
import 'package:tihai/util/network.dart';
import 'package:tihai/util/storage.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({Key? key}) : super(key: key);

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  dynamic user;

  @override
  void initState() {
    super.initState();
    initData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: Card(
        margin: const EdgeInsets.all(10.0),
        child: Column(
          children: user == null
              ? []
              : [
                  Expanded(
                    flex: 1,
                    child: Image.network(user.imageUrl),
                  ),
                  Expanded(flex: 1, child: Text("昵称：${user.username}")),
                  Expanded(
                    flex: 1,
                    child: Text("手机号：${user.phone}"),
                  ),
                  Expanded(
                    flex: 1,
                    child: Text("金币：${user.score}"),
                  ),
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                primary: Colors.redAccent,
                //change background color of button
                onPrimary: Colors.white,
                //change text color of button
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(25),
                ),
              ),
              onPressed: freshData,
              child: const Text("刷新"),
            ),
                ],
        ),
      )),
      floatingActionButton: FloatingActionButton(
        child: const Text("登录"),
        onPressed: () {
          Navigator.pushNamed(context, "/login");
        },
      ),
    );
  }

  initData() async {
    var phone = await Storage.getString("phone");
    Map<String, dynamic> data = {};
    data["phone"] = phone;
    var userBean = await getAPI(UserBean(), API.selectUser, data);
    setState(() {
      user = userBean;
    });
  }

  freshData() {
    postAPI(API.freshProblem, {});
    Fluttertoast.showToast(
        msg: "题库已刷新",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.blueAccent,
        textColor: Colors.white,
        fontSize: 16.0);
  }


}
